/*
 * @lc app=leetcode.cn id=860 lang=cpp
 *
 * [860] 柠檬水找零
 */

// @lc code=start
class Solution
{
public:
  bool lemonadeChange(vector<int>& bills)
  {
    unordered_map<int, int> currency;
    for (int i = 5; i <= 20; i += 5)
      currency[i] = 0;
    for (int& bill : bills) {
      int r = bill - 5;
      // r == 5 or r == 15
      if (r == 5) {
        if (currency[5] > 0)
          --currency[5];
        else
          return false;
      }
      else if (r == 15) {
        if (currency[10] > 0 && currency[5] > 0) {
          --currency[10];
          --currency[5];
        }
        else if (currency[5] >= 3)
          currency[5] -= 3;
        else
          return false;
      }
      ++currency[bill];
    }
    return true;
  }
};
// @lc code=end
